package com.int_tech.newTimeTask;

import com.int_tech.car.dao.CarDao;
import com.int_tech.car.dao.CarInfoDao;
import com.int_tech.car.dao.CarOpenInfoDao;
import com.int_tech.car.entity.Car;
import com.int_tech.car.entity.CarInInfo;
import com.int_tech.car.entity.CarOpenInfo;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.DateTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created by liubch on 2018/5/27.
 */
@Component
public class CCarOpenInfoTimeTask {

    @Autowired
    private CarOpenInfoDao carOpenInfoDao;

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private StationDao stationDao;

    @Autowired
    private CarInfoDao carInfoDao;

    @Scheduled(cron="0/10 * * * * ?")
    public void ansyCarOpenInfo(){

        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByServiceTypeAndCode("M-1","A2");


        if(socketLogSendList == null || socketLogSendList.size() == 0)
        {

        }
        else
        {
            SocketLogSend socketLogSend = socketLogSendList.get(0);

            socketLogSend.setServiceType("M-2");

            socketLogSendDao.save(socketLogSend);

            //基础信息
            String carNumber = socketLogSend.getCarNumber();
            String code = socketLogSend.getCode();
            Date createTime = socketLogSend.getCreateTime();

            String logData = socketLogSend.getDataString();

            String[] logs = logData.split(",");


            //开关门状态
            String openStatus = logs[10];

            //查询车辆相关联的管理员所属的场站信息
            Car car = carDao.findCarByCarNumberAndIsDel(carNumber,0);
            //车辆所属客户
            int clientId = car.getClientId();
            List<Station> stationList = stationDao.findByClientId(clientId);

            //经纬度
            String lng = logs[7];
            String lat = logs[8];

            String time = logs[6];

            int n = 1;
            //开门校验流程
            if("0".equals(openStatus))
            {
                n = 1;
            }
            else if("1".equals(openStatus))
            {
                n = 2;
            }

            //如果是在运营中的任务，判断开门位置，如果在场站内，创建卸货时间，否则异常

            boolean isInStaion = false;
            Station stationIn = new Station();
            //遍历场站数据，拼接场站坐标字符串
            for (Station station:stationList)
            {
                try
                {
                    String partitionLocation = "";

                    //场站坐标数组
                    String[] points = station.getArea().split("&");

                    int m =0;
                    for (String point:points)
                    {
                        try
                        {
                            if(m == 0)
                            {
                                partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                            }
                            else
                            {
                                partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                            }

                            m++;
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

                    //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                    isInStaion = OrderMapTest.isInPolygon(lat,lng,partitionLocation);

                    if(isInStaion)
                    {
                        stationIn = station;
                        break;
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

            //在场站内，更新任务状态
            if(isInStaion)
            {

                //录入入港信息
                CarInInfo carInInfo = new CarInInfo();
                carInInfo.setCarNumber(carNumber);
                carInInfo.setInTime(DateTool.Date2String(createTime,"yyyy-MM-dd HH:mm:ss"));
                //场站id需要查询
                carInInfo.setStationId(stationIn.getId());
                carInInfo.setStationName(stationIn.getName());
                carInInfo.setCreateTime(new Date());
                carInInfo.setStatus(n);

                carInfoDao.save(carInInfo);
            }
            else
            {
                //异常开门
                CarOpenInfo carOpenInfo = new CarOpenInfo();
                carOpenInfo.setCarNumber(carNumber);
                carOpenInfo.setOpenStatus(n);

                //存储起点位置名称
                String add = AddressParse.getAdd(lat,lng);
                JSONObject jsonObject = JSONObject.fromObject(add);
                JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
                JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
                String endAddress = j_2.getString("name");
                carOpenInfo.setAddress(endAddress);
                carOpenInfo.setTime(time);
                carOpenInfo.setCreateTime(new Date());
                carOpenInfo.setStationId(car.getStationId());
                carOpenInfo.setStationName(car.getStationName());
                carOpenInfo.setLat(lat);
                carOpenInfo.setLng(lng);

                carOpenInfoDao.save(carOpenInfo);
            }
        }

    }
}
